package com.github.permissiondog.cancanpbackend.servlet;

import com.github.permissiondog.cancanpbackend.request.TemplateCreateRequest;
import com.github.permissiondog.cancanpbackend.request.TemplateDeleteRequest;
import com.github.permissiondog.cancanpbackend.request.TemplateModifyRequest;
import com.github.permissiondog.cancanpbackend.response.BaseResponse;
import com.github.permissiondog.cancanpbackend.response.TemplateResponse;
import com.github.permissiondog.cancanpbackend.response.TemplatesResponse;
import com.github.permissiondog.cancanpbackend.service.TemplateService;
import org.apache.ibatis.exceptions.PersistenceException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AdminTemplateServlet", value = "/admin/template")
public class AdminTemplateServlet extends BaseServlet{
    /**
     * 创建空审批模板
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        TemplateCreateRequest body = bind(req, res, TemplateCreateRequest.class);
        if (body == null)   return;

        try {
            var template = TemplateService.getInstance().createTemplate(body.tableName, userLoggedIn(req).getId());
            json(res, TemplateResponse.builder().ok(true).template(template).build());
        } catch (PersistenceException e) {
            json(res, BaseResponse.builder().ok(false).msg("创建失败").error(e.getMessage()).build());
        }
    }

    /**
     * 修改审批模板信息
     */
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse res) throws IOException {
        TemplateModifyRequest body = bind(req, res, TemplateModifyRequest.class);
        if (body == null)   return;

        try {
            var template = TemplateService.getInstance().getTemplateById(body.templateId);
            if (template == null) {
                json(res, BaseResponse.builder().ok(false).msg("未找到模板").build());
                return;
            }
            template.setTableName(body.tableName);
            template.setDescription(body.description);
            TemplateService.getInstance().modifyTemplate(template);
        } catch (PersistenceException e) {
            json(res, BaseResponse.builder().ok(false).msg("修改失败").error(e.getMessage()).build());
            return;
        }
        json(res, BaseResponse.builder().ok(true).build());
    }

    /**
     * 删除审批模板
     */
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws IOException {
        TemplateDeleteRequest body = bind(req, res, TemplateDeleteRequest.class);
        if (body == null)   return;

        try {
            var ok = TemplateService.getInstance().deleteTemplate(body.templateId);
            if (!ok) {
                json(res, BaseResponse.builder().ok(false).msg("未找到 id").build());
                return;
            }
        } catch (PersistenceException e) {
            json(res, BaseResponse.builder().ok(false).msg("删除失败").error(e.getMessage()).build());
            return;
        }
        json(res, BaseResponse.builder().ok(true).build());
    }

}
